﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using FireStarterModeller.BO;
using Habanero.Base;
using Habanero.Base.Exceptions;
using Habanero.BO;

namespace FireStarterModeller.Logic.FsStartLoading
{
    /// <summary>
    /// 
    /// </summary>
    public class FsStartLoader
    {

/*
        public static void LoadDataStoreFromFile(string fileName)
        {
            //ResetDataAccessor();
            //_dataStoreInMemory.AllObjects = new FsStartLoader().LoadDataStoreFromFile(fileName);
            FMDataStore.Reset();
            FMDataStore.FMDataStoreInMemory.AllObjects = new FsStartLoader().LoadDataStoreFromFile(fileName);
        }*/

        /// <summary>
        /// Loads the BusinessObjects from a datastore.
        /// The datastore is expected to be a store in the format of 
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public Dictionary<Guid, IBusinessObject> LoadDataStoreFromFile(string fileName)
        {

            //_dataStoreInMemory.LoadFromXml(fileName, typeof(DMSolution));
            using (FileStream fileStream = File.OpenRead(fileName))
            {
                return LoadDataStore(fileStream);
            }
        }

        protected static Dictionary<Guid, IBusinessObject> LoadDataStore(Stream stream)
        {
            try
            {
                //TODO Mark 08 Sep 2009: There must be a better way of doing this
                //These setting are used for controlling the construction of Relationships and Relationship Props.
                //By Default these are created with their reverse relationship but when loading from an fsstart.
                //You do not have this.
                DMRelationship.Loading = true;
                DMRelationshipProperty.Loading = true;
                DataStoreInMemoryXmlReader dataStoreInMemoryXmlReader = new DataStoreInMemoryXmlReader(stream);
                return dataStoreInMemoryXmlReader.Read();
            }
            catch (XmlException)
            {
                //throw new HabaneroApplicationException("There is no solution loaded from the selected file '" + fileName + "'");
                throw new HabaneroApplicationException("Unable to load the solution from the selected source");
            }
            finally
            {
                DMRelationship.Loading = false;
                DMRelationshipProperty.Loading = false;
            }
        }


        public void SaveDataStoreToFile(DataStoreInMemory dataStore,  string fileName)
        {
            if (dataStore == null) return;
            //_dataStoreInMemory.SaveToXml(fileName);
            using (FileStream fileStream = File.Create(fileName))
            {
                XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
                xmlWriterSettings.ConformanceLevel = ConformanceLevel.Auto;
                xmlWriterSettings.Indent = true;
                DataStoreInMemoryXmlWriter dataStoreInMemoryXmlWriter = new DataStoreInMemoryXmlWriter(fileStream, xmlWriterSettings);
                dataStoreInMemoryXmlWriter.Write(dataStore);
                XmlWriter xmlWriter = XmlWriter.Create(fileStream, xmlWriterSettings);
                xmlWriter.WriteString("\r\n");
                xmlWriter.WriteElementString("Version", "3.1b");
                xmlWriter.Close();

            }
        }
    }
}
